package com.daxt.common.ureport;

import com.bstek.ureport.console.UReportServlet;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @description: UReport配置
 *
 * @author: lig
 * @date: 2021/5/21
 */
@Configuration
public class UreportConfig implements EnvironmentAware {

    private Environment environment;



    //是否开启设计器  lig  2021-06-07
    private boolean designer = false;


    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    @Bean
    public UReportPropertyConfigurer propertyrConfigurer() {
        String activeProfile = environment.getProperty("spring.profiles.active");
        if (StringUtils.isNotBlank(activeProfile)) {
            activeProfile = "-" + activeProfile;
        }
        if(activeProfile.equals("-dev") ||activeProfile.equals("-test")||activeProfile.equals("-win")||activeProfile.equals("-lq")||activeProfile.equals("-zy")||activeProfile.equals("-ws")){
            designer = true;

        }
        return new UReportPropertyConfigurer("application" + activeProfile + ".yml");
    }

    @Bean
    public ServletRegistrationBean<UReportServlet> ureportServlet() {
        ServletRegistrationBean<UReportServlet> bean = new ServletRegistrationBean<>(new UReportServlet());

        //根据debug 开启或关闭控制台
        if(designer){
            List<String> umList = new ArrayList<String>(Arrays.asList(ureportMapping));
            umList.add( "/ureport/designer/*");
            bean.addUrlMappings(umList.toArray(new String[umList.size()]));
        }else{
            bean.addUrlMappings(ureportMapping);
        }
        return bean;
    }


    /**
     * @description: 上线需要注释控制台地址
     *
     * @author: lig
     * @date: 2021/6/7
     */
    public static final String[] ureportMapping = {
            "/ureport/chart/*",
            "/ureport/datasource/*",
            "/ureport/preview/*",
            "/ureport/look/*",
//            "/ureport/designer/*",
            "/ureport/import/*",
            "/ureport/searchFormDesigner/*",
            "/ureport/res/*",
            "/ureport/excel97/*",
            "/ureport/excel/*",
            "/ureport/pdf/*",
            "/ureport/word/*",
            "/ureport/xml/*",
    };


}
